<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport"
		content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
	<title>登录</title>
	<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
	<style>
		p {
			margin-bottom: 15px;
		}
	</style>
</head>

<body>

	<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
		<a class="aui-pull-left aui-btn">
		</a>
		<div class="aui-title">系统登录</div>
		<a class="aui-pull-right aui-btn " onclick="setting()">
			<span class="aui-iconfont aui-icon-gear"></span>
		</a>
	</header>

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-media-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-row aui-row-padded">
						<div class="aui-col-xs-4">
						</div>
						<div class="aui-col-xs-4">
							<!-- <img src="../image/logo-right.png" /> -->
						</div>
						<div class="aui-col-xs-4">
						</div>
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="aui-content aui-margin-b-15">
		<ul class="aui-list aui-form-list">
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label-icon">
						<i class="aui-iconfont aui-icon-my"></i>
					</div>
					<div class="aui-list-item-input">
						<input type="text" placeholder="username" id="username" value="" />
					</div>
					<div class="aui-list-item-label-icon" id="clearUn" style="display: none" onclick="clearUnInput()">
						<i class="aui-iconfont aui-icon-close"></i>
					</div>
				</div>
			</li>
			<li class="aui-list-item">
				<div class="aui-list-item-inner">
					<div class="aui-list-item-label-icon">
						<i class="aui-iconfont aui-icon-lock"></i>
					</div>
					<div class="aui-list-item-input">
						<input type="password" placeholder="password" id="password" value="" />
					</div>
					<!--<div class="aui-list-item-label-icon" id="clearPwd" style="display: none" onclick="clearPwdInput()">
	                        <i class="aui-iconfont aui-icon-close"></i>
	                    </div>-->
					<div class="aui-list-item-label-icon" id="pwdChange" onclick="changePwdCls()">
						<i class="aui-iconfont aui-icon-hide" id="pwdIcon"></i>
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="aui-content-padded">
		<p>
		<div class="aui-btn aui-btn-info aui-btn-block" onclick='login()'>登录</div>
		<!-- <div style="text-align: center;padding-top: 5px;color:#03a9f4" onclick="loginface()">人脸登录</div> -->
		</p>
	</div>

	<footer class="aui-bar aui-bar-tab" id="aui-footer">
		<div class="aui-bar-tab-item" tapmode>
			<ul class="aui-list aui-media-list">
				<li class="aui-list-item">
					<div class="aui-list-item-inner">
						<div class="aui-row aui-row-padded">
							<div class="aui-col-xs-4">
							</div>
							<div class="aui-col-xs-4">
								<!-- <img src="../image/logo-right.png" /> -->
							</div>
							<div class="aui-col-xs-4">
							</div>
						</div>
					</div>
				</li>
			</ul>
		</div>
	</footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
	var pwdClsFlag = 0;
	var oldUn = "";
	var unInput = document.querySelector("#username");
	var unClear = document.querySelector("#clearUn");
	var pwdInput = document.querySelector("#password");
	//		var pwdClear = document.querySelector("#clearPwd");

	unInput.oninput = function () {
		if (this.value.length) {
			unClear.style.display = 'block';
		} else {
			unClear.style.display = 'none';
		}
	}

	unInput.onclick = function () {
		if (this.value.length) {
			unClear.style.display = 'block';
		} else {
			unClear.style.display = 'none';
		}
	}

	pwdInput.onfocus = function () {
		unClear.style.display = 'none';
	}

	function changePwdCls() {
		if (pwdClsFlag == 0) {
			//显示出密码
			pwdClsFlag = 1;
			document.getElementById("password").type = "text";
			document.getElementById('pwdIcon').setAttribute("class", "aui-iconfont aui-icon-display");
		} else {
			//隐藏密码
			pwdClsFlag = 0;
			document.getElementById("password").type = "password";
			document.getElementById('pwdIcon').setAttribute("class", "aui-iconfont aui-icon-hide");
		}
		unClear.style.display = 'none';
	}

	apiready = function () {
		ready();
	}

	function ready() {
		getAppVer();
		api.setScreenOrientation({
			orientation: 'auto'
		});
		api.addEventListener({
			name: 'keyback'
		}, function (ret, err) {
			if (ret) {
				api.closeWin();
				api.setScreenOrientation({
					orientation: 'portrait_up'
				});
			} else {
				alert(JSON.stringify(err));
			}
		});

		oldUn = localStorage.getItem('un');
		if (oldUn != null) {
			$("#username").attr("value", oldUn);
		}
	};

	function clearUnInput() {
		//清空输入框内容
		$("#username").val('');
		//隐藏清空按钮
		unClear.style.display = 'none';
		$("#username").focus();
	}

	function closeWin() {
		api.closeWin();
	}

	function setting() {
		api.openWin({
			name: "settings",
			url: "settings.html"
		});
	};

	function login() {
		var username = $("#username").val();
		var password = $("#password").val();

		if (username == null || username == undefined || username == "") {
			api.toast({
				msg: "用户名不能为空",
				duration: 3000
			});
			return;
		}

		if (password == null || password == undefined || password == "") {
			api.toast({
				msg: "密码不能为空",
				duration: 3000
			});
			return;
		}

		api.showProgress({
			title: '登录中...'
		});
		var username = $("#username").val();
		var password = $("#password").val();
		//var serverUrl = $api.getStorage("serverUrl");

		//先获取localStorage中的配置的内外网IP检测网络是否畅通（内网优先）
		var serverIp = "";
		var serverPort = "";
		var serverProject = "";

		var serverIpN = localStorage.getItem('serverIpN');

		//判断是内网还是外网配置
		var netChoice = localStorage.getItem('netChoice');
		if (netChoice == "out") {
			//外网访问
			serverIp = localStorage.getItem('serverIpW');
			serverPort = localStorage.getItem('serverPortW');;
			serverProject = localStorage.getItem('serverProjectW');
		} else {
			//内网访问
			serverIp = localStorage.getItem('serverIpN');
			serverPort = localStorage.getItem('serverPortN');;
			serverProject = localStorage.getItem('serverProjectN');
		}

		doLogin(serverIp, serverPort, serverProject, username, password);
	}

	//登录
	function doLogin(serverIp, serverPort, serverProject, username, password) {
		var url = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/app/login";
		var data = {
			values: { ////以表单方式提交参数（JSON对象）
				_u: username,
				_p: password
			}
		};
		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 30,
			returnAll: false,
			data: data
		}, function (ret, err) {
			api.hideProgress();
			if (ret) {
				var data = JSON.stringify(ret);
				var map = eval("(" + data + ")");

				//用户名密码验证成功
				if (map.code == "0") {
					localStorage.setItem('un', username);
					localStorage.setItem('serverIp', serverIp);
					localStorage.setItem('serverPort', serverPort);
					localStorage.setItem('serverProject', serverProject);
					var serverUrl = "http://" + serverIp + ":" + serverPort + "/" + serverProject;
					localStorage.setItem('serverUrl', serverUrl);
					localStorage.setItem('tenantId', map.tenantId);

					api.sendEvent({

						name: 'loginSuccess'

					});

					api.openWin({
						name: "menu",
						url: "menu.html"
					});

				} else {
					api.toast({
						msg: map.message,
						duration: 2000
					});
				}
			} else {
				alert(JSON.stringify(err))
			}
		});
	}

	//获取APP版本
	function getAppVer() {
		var serverIp = "";
		var serverPort = "";
		var serverProject = "";
		//判断是内网还是外网配置
		var netChoice = localStorage.getItem('netChoice');
		if (netChoice == "out") {
			//外网访问
			serverIp = localStorage.getItem('serverIpW');
			serverPort = localStorage.getItem('serverPortW');;
			serverProject = localStorage.getItem('serverProjectW');
		} else {
			//内网访问
			serverIp = localStorage.getItem('serverIpN');
			serverPort = localStorage.getItem('serverPortN');;
			serverProject = localStorage.getItem('serverProjectN');
		}
		var url = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/app/getAppVer";
		var oldVer = api.appVersion; //应用版本号
		// alert("APP版本：" + oldVer);
		var appname = "HKAPP_";//apk名称
		var data = {
			values: {
				ver: oldVer,
				appName: appname
			}
		};

		api.ajax({
			url: url,
			method: 'post',
			dataType: 'json',
			timeout: 30,
			returnAll: false,
			data: data
		}, function (ret, err) {
			if (ret) {
				var data = JSON.stringify(ret);
				var map = eval("(" + data + ")");

				var retData = map.result.split(":");
				if (retData[0] == "S") {
					var ver = retData[1]; //版本号
					alert("APP最新版本：" + ver);
					checkUpdate(serverIp, serverPort, serverProject, ver);
				} else {
					// console.log(retData[1]);
				}
			} else {
				// alert(JSON.stringify(err));
				api.confirm({
					title: '设置网络',
					msg: '网络不通,请先设置访问网络',
					buttons: ['确定', '取消']
				}, function (ret, err) {
					var index = ret.buttonIndex;
					if (index == 1) {
						api.openWin({
							name: "settings",
							url: "settings.html"
						});
					} else {
						api.closeWidget({
							silent: true
						});
					}
				});
			}
		});
	}

	//强制更新
	function checkUpdate(serverIp, serverPort, serverProject, ver) {
		var msg = "版本号：" + ver;
		api.confirm({
			title: '强制更新APP',
			msg: msg,
			buttons: ['确定', '取消']
		}, function (ret, err) {
			var index = ret.buttonIndex;
			if (index == 1) {
				var downloadUrl = "http://" + serverIp + ":" + serverPort + "/download/" + "HKAPP_" + ver + ".apk";//HKAPP_
				// var url = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/app/download";
				console.log(downloadUrl);
				api.download({
					url: downloadUrl,
					report: true,
					savePath: 'fs://' + 'HKAPP_' + ver + '.apk',
				}, function (ret, err) {
					if (ret && 0 == ret.state) { /* 下载进度 */
						api.toast({
							msg: "正在下载应用" + ret.percent + "%",
							duration: 10000
						});
					}

					if (ret && 1 == ret.state) { /* 下载完成 */
						var savePath = ret.savePath;
						// alert(ret.savePath);
						api.installApp({
							appUri: savePath
						});
					}

					if (ret && 2 == ret.state) {
						api.toast({
							msg: "下载失败",
							duration: 10000
						});
					}
				});
			} else {
				api.closeWidget({
					silent: true
				});
			}
		});
	}

	function loginface() {

		faceDetect({ debug: 0 }, (ret) => {

			if (ret.status) {
				var serverIp = "";
				var serverPort = "";
				var serverProject = "";

				var serverIpN = localStorage.getItem('serverIpN');
				serverIp = localStorage.getItem('serverIpN');
				serverPort = localStorage.getItem('serverPortN');;
				serverProject = localStorage.getItem('serverProjectN');

				var url = "http://" + serverIp + ":" + serverPort + "/" + serverProject + "/app/facelogin";
				var data = {
					values: {
						faceBase64: ret.face,
						padCode:localStorage.getItem('equipUniqueId'),
					}
				};
				api.ajax({
					url: url,
					method: 'post',
					dataType: 'application/x-www-form-urlencoded',
					timeout: 30,
					returnAll: false,
					data: data
				}, function (ret, err) {
					if (ret) {

						var map = JSON.parse(ret);

						if (map.code == 0) {
							console.log(map.code);
							if (map.username != null && map.username != "") {
								localStorage.setItem('un', map.username);
							}
							localStorage.setItem('serverIp', serverIp);
							localStorage.setItem('serverPort', serverPort);
							localStorage.setItem('serverProject', serverProject);
							var serverUrl = "http://" + serverIp + ":" + serverPort + "/" + serverProject;

							localStorage.setItem('serverUrl', serverUrl);

							api.sendEvent({
								name: 'loginSuccess'
							});

							api.openWin({
								name: "menu",
								url: "menu.html"
							});

						} else {
							api.toast({
								msg: map.msg,
								duration: 2000
							});
						}
					}
				});
			} else {
				console.log(ret);
			}
		});
	}

	function faceDetect({ debug = 0, cropType = 3, cropHeight = 300, quality = 70 }, callback) {
		const module = api.require('dwzBaiduFaceLive');
		module.faceDetect(
			{
				debug: debug, // 调试开关(默认:0)：0, 1
				cropType: cropType, // 抠图类型(默认:1)：1:脸部, 2:大头照, 3:头像+肩膀
				cropHeight: cropHeight, // 抠图高的设定，为了保证好的抠图效果，要求高宽比是4:3，所以会在内部进行计算，只需要传入高即可，取值范围50 ~ 1200，默认480
				quality: quality // 抠图压缩质量，取值范围 20 ~ 100，默认100不压缩
			},
			(ret, err) => {
				if (!ret.status && ret.message) {
					$.alert.toast(ret.message);
				}

				callback && callback(ret);
			}
		);
	}
</script>

</html>